package com.jtny.nytb.datafilling.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jtny.nytb.datafilling.dao.EnergyModelDAO;
import com.jtny.nytb.datafilling.dao.EnergyModelPriceDAO;
import com.jtny.nytb.datafilling.domain.EnergyModelDO;
import com.jtny.nytb.datafilling.domain.EnergyModelPriceDO;
import com.jtny.nytb.datafilling.dto.*;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.jtny.component.exception.ServiceException;
import com.jtny.component.util.BeanUtils;

import com.jtny.nytb.datafilling.service.EnergyPriceTempService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 具体实现EnergyPriceTempService定义的接口
 *
 * @author 罗霄林
 * @package com.jtny.nytb.energymonitor.datafilling.service.impl
 * @fileName EnergyPriceTempServiceImpl.java
 * @createTime 2023年09月05日 12:18
 * 版权所有
 */
@Service
@Slf4j
public class EnergyPriceTempServiceImpl extends ServiceImpl<EnergyModelPriceDAO, EnergyModelPriceDO> implements EnergyPriceTempService {

    @Autowired
    private EnergyModelDAO energyModelDAO;

    @Autowired
    private EnergyModelPriceDAO energyModelPriceDAO;

    @Override
    public List<EnergyPriceTempletDTO> queryEnergyPriceTempletInfo(EnergyPriceTempletParamDTO energyPriceTempletParamDTO) {
        return energyModelDAO.queryEnergyPriceTemplet(energyPriceTempletParamDTO);
    }

    @Override
    public Map<String, List<String>> queryInstitution(String typeId) {
        Map<String, List<String>> map = new HashMap<>();
        LambdaQueryWrapper<EnergyModelDO> queryWrapper = Wrappers.lambdaQuery(EnergyModelDO.class)
                .eq(EnergyModelDO::getTypeId, typeId).eq(EnergyModelDO::getStatus, "0");
        //根据能源类型查询所有的价格模板数据
        List<EnergyModelDO> list = energyModelDAO.selectList(queryWrapper);
        if (ObjectUtils.isNotEmpty(list)) {
            List<String> institutionList = new ArrayList<>();
            List<String> nameList = new ArrayList<>();
            //获取制度 放入set去重
            institutionList.addAll(list.stream().map(EnergyModelDO::getInstitution).collect(Collectors.toSet()));
            map.put("institution", institutionList);
        }
        return map;
    }


    @Override
    @Transactional
    public Boolean saveEnergyPriceTempletData(EnergyPriceTempletParamDTO energyPriceTempletParamDTO) {
        EnergyModelDO energyModelDO = new EnergyModelDO();
        BeanUtils.copyObject(energyPriceTempletParamDTO, energyModelDO);
        if (energyModelDAO.insert(energyModelDO) == 1) {
            List<EnergyModelPriceDO> list = BeanUtils.copyToList(energyPriceTempletParamDTO.getDataList(), EnergyModelPriceDO.class);
            list.forEach(
                    ep -> {
                        if (ep.getColumnValue() == null) {
                            ep.setColumnValue(null);
                        }
                        ep.setEnergyModelId(energyModelDO.getId());
                        ep.setStatus(0);
                    }
            );
            int num = energyModelPriceDAO.insertBatch(list);
            return num == energyPriceTempletParamDTO.getDataList().size();
        }
        return false;
    }

    @Override
    @Transactional
    public void modfiyEnergyPriceTempletData(EnergyPriceTempletParamDTO energyPriceTempletParamDTO) {
        int failNum = 0;
        EnergyModelDO energyModelDO = new EnergyModelDO();
        BeanUtils.copyObject(energyPriceTempletParamDTO, energyModelDO);
        if (energyModelDAO.updateById(energyModelDO) == 1) {
            List<EnergyPriceDTO> list = energyPriceTempletParamDTO.getDataList();
            for (EnergyPriceDTO epDTO : list) {
//                LambdaUpdateWrapper<EnergyModelPriceDO> updateWrapper = Wrappers.lambdaUpdate(EnergyModelPriceDO.class)
//                        .eq(EnergyModelPriceDO::getEnergyModelId, energyPriceTempletParamDTO.getId())
//                        .eq(EnergyModelPriceDO::getColumnName, epDTO.getColumnName());
//                EnergyModelPriceDO energyModelPriceDO = new EnergyModelPriceDO();
//                energyModelPriceDO.setColumnValue(epDTO.getColumnValue());
                UpdateWrapper<EnergyModelPriceDO> objectUpdateWrapper = new UpdateWrapper<>();
                objectUpdateWrapper.lambda().set(EnergyModelPriceDO::getColumnValue, epDTO.getColumnValue())
                        .eq(EnergyModelPriceDO::getEnergyModelId, energyPriceTempletParamDTO.getId())
                        .eq(EnergyModelPriceDO::getColumnName, epDTO.getColumnName());
                this.update(objectUpdateWrapper);
            }
        }
    }

    @Override
    public Boolean delEnergyPriceTempletData(String id) {
        EnergyModelDO energyModelDO = new EnergyModelDO();
        energyModelDO.setStatus(9);
        energyModelDO.setId(Long.parseLong(id));
        if (energyModelDAO.updateById(energyModelDO) == 1) {
            EnergyModelPriceDO energyModelPriceDO = new EnergyModelPriceDO();
            energyModelPriceDO.setStatus(9);
            energyModelPriceDO.setId(Long.parseLong(id));
            if (energyModelPriceDAO.updateById(energyModelPriceDO) != 1) {
                return false;
            }
        }
        return true;
    }

    @Override
    @Transactional
    public Boolean importEnergyPriceTempletData(List<EnergyPriceTempletParamDTO> list) {
        List<EnergyModelDO> EnergyModelDOList = BeanUtils.copyToList(list, EnergyModelDO.class);
        EnergyModelDOList.forEach(
                emd -> {
                    energyModelDAO.insert(emd);
                }
        );
        for (int i = 0; i < list.size(); i++) {
            List<EnergyPriceDTO> dataList = list.get(i).getDataList();
            List<EnergyModelPriceDO> EnergyModelPriceDOList = BeanUtils.copyToList(dataList, EnergyModelPriceDO.class);
            Long id = EnergyModelDOList.get(i).getId();
            EnergyModelPriceDOList.forEach(
                    emp -> {
                        emp.setStatus(0);
                        emp.setEnergyModelId(id);
                    }
            );
            energyModelPriceDAO.insertBatch(EnergyModelPriceDOList);
        }
        return true;
    }

    @Override
    public NameOrGroupContentDTO queryNameByInstitutionAndType(String typeId, String institution) {
        NameOrGroupContentDTO ngcDto = new NameOrGroupContentDTO();
        LambdaQueryWrapper<EnergyModelDO> queryWrapper = Wrappers.lambdaQuery(EnergyModelDO.class)
                .eq(EnergyModelDO::getInstitution, institution)
                .eq(EnergyModelDO::getTypeId, typeId)
                .eq(EnergyModelDO::getStatus, "0");
        List<EnergyModelDO> list = energyModelDAO.selectList(queryWrapper);
        Set<String> groupSet = list.stream().map(EnergyModelDO::getName).collect(Collectors.toSet())
                .stream().filter(str -> StringUtils.isNotEmpty(str)).collect(Collectors.toSet());
        if (ObjectUtils.isNotEmpty(groupSet)) {
            List<String> groupList = new ArrayList<>();
            groupList.addAll(groupSet);
            ngcDto.setType("group");
            ngcDto.setContentList(groupList);
            return ngcDto;
        }
        return ngcDto;
    }

    @Override
    public List<EnergyModelDTO> getEnergyPriceTempletInfo(EnergyModelPriceParamDTO energyModelPriceParamDTO) {
        if (ObjectUtil.isEmpty(energyModelPriceParamDTO)) {
            return new ArrayList<>();
        }
        List<EnergyModelDO> energyModelDOList = energyModelDAO.getEnergyPriceTempletInfo(energyModelPriceParamDTO);
//        energyModelDOList.forEach(a ->{
//            a.getEnergyModelPriceList().forEach(b ->{
//                if (b.getStatus() == 9) {
//                    a.setEnergyModelPriceList(new ArrayList<>());
//                }
//            });
//        });
        return BeanUtils.copyToList(energyModelDOList, EnergyModelDTO.class);
    }

    @Override
    public void deleteEnergyTemplate(List<EnergyModelPriceDTO> energyModelPriceDTOS) {
        if (CollectionUtil.isEmpty(energyModelPriceDTOS)) {
            return;
        }
        UpdateWrapper<EnergyModelPriceDO> objectUpdateWrapper = new UpdateWrapper<>();
        objectUpdateWrapper.lambda().set(EnergyModelPriceDO::getColumnValue, null)
                .eq(EnergyModelPriceDO::getEnergyModelId,energyModelPriceDTOS.get(0).getEnergyModelId());
        this.update(objectUpdateWrapper);
    }

    @Override
    public List<EnergyModelPriceDTO> seleteEnergyTemplatePrice(Long id) {
        if (id == null) {
            throw new ServiceException("id不能为空");
        }
        LambdaQueryWrapper<EnergyModelPriceDO> energyModelPriceQueryWrapper = new LambdaQueryWrapper<>();
        energyModelPriceQueryWrapper.eq(EnergyModelPriceDO::getEnergyModelId, id);
        List<EnergyModelPriceDO> energyModelPriceDOS = energyModelPriceDAO.selectList(energyModelPriceQueryWrapper);
        return BeanUtils.copyToList(energyModelPriceDOS, EnergyModelPriceDTO.class);
    }

    @Override
    public List<EnergyModelDTO> getEnergyTemplateInfo(Long id) {
        if (id == null) {
            throw new ServiceException("id不能为空");
        }
        List<EnergyModelDO> energyModelDOList = energyModelDAO.getEnergyTemplateInfo(id);
        return BeanUtils.copyToList(energyModelDOList, EnergyModelDTO.class);
    }

    @Override
    public List<EnergyModelPriceDTO> getEnergyPrice(Long id) {
        if (ObjectUtil.isEmpty(id)) {
            throw new ServiceException("模板id不能为空");
        }
        LambdaQueryWrapper<EnergyModelPriceDO> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(EnergyModelPriceDO::getEnergyModelId, id);
        List<EnergyModelPriceDO> energyModelPriceDOS = energyModelPriceDAO.selectList(objectLambdaQueryWrapper);
        return BeanUtils.copyToList(energyModelPriceDOS, EnergyModelPriceDTO.class);
    }

    @Override
    @Transactional
    public void saveEnergyPrice(EnergyPriceTempletParamDTO eqtDTO) {
        EnergyModelDO energyModelDO = new EnergyModelDO();
        BeanUtils.copyObject(eqtDTO, energyModelDO);
        if (energyModelDAO.updateById(energyModelDO) == 1) {
            List<EnergyModelPriceDO> list = BeanUtils.copyToList(eqtDTO.getDataList(), EnergyModelPriceDO.class);
            list.forEach(
                    ep -> {
                        if (ep.getColumnValue() == null) {
                            ep.setColumnValue(null);
                        }
                        ep.setEnergyModelId(energyModelDO.getId());
                        ep.setStatus(0);
                    }
            );
            energyModelPriceDAO.insertBatch(list);
        }
    }

    @Override
    public Boolean deleteEnergyTemplateById(Long id) {
        return SqlHelper.retBool(energyModelDAO.deleteById(id));
    }

}
